package cn.lystudio.rohm_time_op.controller.appApi;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.util.ObjectUtil;
import cn.lystudio.rohm_time_op.entity.po.UsmBtRecordPO;
import cn.lystudio.rohm_time_op.service.UsmBtRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/app-api/usmbtrecord")
@CrossOrigin
@SaIgnore
@Slf4j
public class AppUsmBtRecordController {
    private UsmBtRecordService usmBtRecordService;

    @Autowired
    public void setUsmBtRecordService(UsmBtRecordService usmBtRecordService) {
        this.usmBtRecordService = usmBtRecordService;
    }
    @PostMapping("/addUsmBtRecord")
    public SaResult addUsmBtRecord(@RequestBody UsmBtRecordPO usmBtRecordPO){
        int res = usmBtRecordService.addRecord(usmBtRecordPO);
        if(res<1) return SaResult.error("添加失败");
        return SaResult.ok();
    }

    @GetMapping("/getUsmBtRecordUnfinished")
    public SaResult getUsmBtRecord(String machineNo,String lotNo){
        UsmBtRecordPO usmBtRecordPO = new UsmBtRecordPO();
        usmBtRecordPO.setMachineNo(machineNo);
        UsmBtRecordPO res = usmBtRecordService.getRecordUnfinished(usmBtRecordPO);
        if(ObjectUtil.isEmpty(res)) return SaResult.error("新记录");
        return SaResult.data(res);
    }

    @GetMapping("/getFinishedRecordByLotNo")
    public SaResult getFinishedRecordByLotNo(String lotNo){
        return SaResult.data(usmBtRecordService.getFinishedRecordByLotNo(lotNo));
    }

    @GetMapping("/getUsmBtRecordByID")
    public  SaResult getUsmBtRecordByID(String recordID){
        UsmBtRecordPO record = usmBtRecordService.getUsmBtRecordByID(recordID);
        if(ObjectUtil.isEmpty(record)) return SaResult.error("数据获取异常");
        return SaResult.data(record);
    }

    @PostMapping("/uptUsmBtRecord")
    public SaResult uptUsmBtRecord(@RequestBody UsmBtRecordPO usmBtRecordPO){
        int res = usmBtRecordService.uptRecord(usmBtRecordPO);
        if(res<1) return SaResult.error("更新失败");
        return SaResult.ok();
    }

    @PostMapping("/delUsmBtRecord")
    public SaResult delUsmBtRecord(@RequestBody UsmBtRecordPO usmBtRecordPO){
        int res = usmBtRecordService.delRecord(usmBtRecordPO.getRecordID());
        if(res<1) return SaResult.error("删除失败");
        return SaResult.ok();
    }
}
